﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Controls;
using System.Windows.Input;
using System.Data;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Command;
using GoldDigger.AppServices;
using GoldDigger.Models;
using GoldDigger.Data;

namespace GoldDigger.ViewModels
{
    public class AssociationRuleResultsViewModel : ViewModelBase
    {
        private MainService _app;
        private ICommand _newProjectCommand;
        private ICommand _exportToGexfCommand;
        private ICommand _backToAssociationRuleCommand;
        private string _selectedInformation;
        private ObservableCollection<AssociationRuleModel> _assocRules;
        private AssociationRuleModel _currentlySelected;

        public AssociationRuleResultsViewModel(MainService ms)
        {
            _app = ms;
            _assocRules = new ObservableCollection<AssociationRuleModel>(_app.AppData.AprioriData.AssociationRules);
            GenerateSummary();
        }

        public ObservableCollection<AssociationRuleModel> AssocRules
        {
            get
            {
                return _assocRules;
            }
        }

        public string SelectedInformation
        {
            get
            {
                return _selectedInformation;
            }
        }

        public AssociationRuleModel CurrentlySelected
        {
            get
            {
                return _currentlySelected;
            }
            set
            {
                _currentlySelected = value;
                RaisePropertyChanged("CurrentlySelected");
            }
        }

        public ICommand NewProjectCommand
        {
            get
            {
                return _newProjectCommand ?? (_newProjectCommand = new RelayCommand(NewProject));
            }
        }

        public ICommand ExportToGexfCommand
        {
            get
            {
                return _exportToGexfCommand ?? (_exportToGexfCommand = new RelayCommand(ExportToGexf));
            }
        }

        public ICommand BackToAssociationRuleCommand
        {
            get
            {
                return _backToAssociationRuleCommand ?? (_backToAssociationRuleCommand = new RelayCommand(BackToAssociationRule));
            }
        }

        public void NewProject()
        {
            _app = new MainService();
            Messenger.Default.Send(new NotificationMessage<string>("summary", "UpdateMenu"));
            Messenger.Default.Send(new NotificationMessage<string>("export", "UpdateMenu"));
            Messenger.Default.Send(new NotificationMessage<ViewModelBase>(new StartViewModel(_app), "ChangeVM"));
        }

        public void ExportToGexf()
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "Save a GEXF File";
            saveFileDialog.Filter = "GEXF File|*.gexf";
            saveFileDialog.ShowDialog();

            // If the file name is not an empty string open it for saving.
            if (saveFileDialog.FileName != "")
            {
                _app.ExportResultsToGexf(saveFileDialog.FileName, _currentlySelected, ExportComplete);
            }
        }

        public void ExportComplete(bool complete)
        {
        }

        public void GenerateSummary()
        {
            if (_app.AppData.AprioriData.AssociationRules.Count == 0)
                _selectedInformation = "No association rules where found for the specified parameters.";
            else
                _selectedInformation = "";
        }

        public void BackToAssociationRule()
        {
            _app.AppData.AprioriData = new AprioriHolder();
            Messenger.Default.Send(new NotificationMessage<ViewModelBase>(new AssociationRuleViewModel(_app), "ChangeVM"));
        }
    }
}
